﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Maticsoft.DBUtility;
using System.Data;
using System.Data.SqlClient;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// PaperAnswerOption数据类
    /// </summary>
    public class PaperAnswerOption
    {
        
        /// <summary>
        /// 获得数据列表
        /// </summary>
        /// <creator>chenly</creator>
        /// <date>2013-7-25</date>
        /// <param name="strWhere">查询条件</param>
        /// <returns>DataSet表</returns>
        public DataSet GetModelList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FPaperTopicId,FAnswerNo,FAnswerContent,FIsRestsAnswer,FRestsAnswerCount ");
            strSql.Append(" FROM t_PaperAnswerOption ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 删除选择项并且修改该选择项后面的所有选择项的编号 事务模式
        /// </summary>
        /// <creator>chenly</creator>
        /// <date>2013-7-25</date>
        /// <param name="topicId">主题编号</param>
        /// <param name="answerNo">答案</param>
        /// <param name="updAnswerOptionList">答案的集合</param>
        public void DelAndUpdAnswerNo(decimal topicId, string answerNo, List<Model.PaperAnswerOption> updAnswerOptionList)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();

            strSql.Append("delete from t_PaperAnswerOption ");
            strSql.Append(" where FPaperTopicId=@FPaperTopicId and FAnswerNo=@FAnswerNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FPaperTopicId", SqlDbType.Decimal),
					new SqlParameter("@FAnswerNo", SqlDbType.Char,1)};
            parameters[0].Value = topicId;
            parameters[1].Value = answerNo;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            //更新后面的选择项
            if (updAnswerOptionList.Count > 0)
            {
                strSql = new StringBuilder();
                strSql.Append("update t_PaperAnswerOption set ");
                strSql.Append("FAnswerNo=@FAnswerNoNew");
                strSql.Append(" where FPaperTopicId=@FPaperTopicId and FAnswerNo=@FAnswerNo ");
                string strSqls = strSql.ToString();

                foreach (Model.PaperAnswerOption model in updAnswerOptionList)
                {
                    cmds = new CommandInfo();

                    SqlParameter[] parameters1 = {
                            new SqlParameter("@FAnswerNoNew", SqlDbType.Char,1),
                            new SqlParameter("@FPaperTopicId", SqlDbType.Decimal),
                            new SqlParameter("@FAnswerNo", SqlDbType.Char,1)};
                    parameters1[0].Value = model.FAnswerContent;
                    parameters1[1].Value = model.FPaperTopicId;
                    parameters1[2].Value = model.FAnswerNo;

                    cmds.CommandText = strSqls;
                    cmds.Parameters = parameters1;
                    cmdList.Add(cmds);
                }
            }

            DbHelperSQL.ExecuteSqlTranWithString(cmdList);


        }

    }
}
